package com.ccy.springboot149.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ccy.springboot149.entity.UsersEntity;
import com.ccy.springboot149.service.TokenService;
import com.ccy.springboot149.service.UsersService;
import com.ccy.springboot149.utils.MPUtil;
import com.ccy.springboot149.utils.PageUtils;
import com.ccy.springboot149.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

/**
 * @Author : CaiYang
 * @Date : 2025-07-01 23:28
 * @Description : 用户登录相关接口
 */
@RequestMapping("users")
@RestController
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UsersService userServer;
    @Autowired
    private TokenService tokenServer;

//    @Ignore
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public R login(String username, String password, String captcha, HttpServletRequest request){
        // 根据用户名查找用户
        UsersEntity user = userServer.selectOne(new EntityWrapper<UsersEntity>().eq("username",username));
        // 比对用户信息
        if(user == null || !user.getPassword().equals(password)) {
            return R.error("账号或密码不正确");
        }
        // 用户存在，生成token
        String token = tokenServer.generateToken(user.getId(),username,"users",user.getRole());
        R r = R.ok();
        r.put("token",token);
        r.put("role",user.getRole());
        r.put("userId",user.getId());
        return r;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public R register(@RequestBody UsersEntity user){
        // 查询用户是否存在
        if(userServer.selectOne(new EntityWrapper<UsersEntity>().eq("username",user.getUsername())) != null) {
            return R.error("用户存在");
        }
        // 用户不存在，直接插入
        userServer.insert(user);
        return R.ok("用户注册成功");
    }

    /**
     * 用户退出
     * @param request
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation("用户退出")
    public R logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.ok("退出成功");
    }

    /**
     * 密码重置
     * @param name
     * @param request
     * @return
     */
    @RequestMapping(value = "/resetPass")
    @ApiOperation("密码重置")
    public R resetPass(String name,HttpServletRequest request) {
        System.out.println("重置密码用户为"+name);
        UsersEntity user = userServer.selectOne(new EntityWrapper<UsersEntity>().eq("username",name));
        if(user == null) {
            return R.error("账号不存在!");
        }
        user.setPassword("123456");
        userServer.update(user,null);
        return R.ok("密码重置成功！");
    }

    @RequestMapping("/page")
    public R page(@RequestParam Map<String,Object> params, UsersEntity user) {
        EntityWrapper<UsersEntity> ew = new EntityWrapper<UsersEntity>();
        PageUtils page = userServer.
                queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.allLike(ew, user), params), params));
        return R.ok().put("data", page);
    }

    @RequestMapping("/save")
    public R save(@RequestBody UsersEntity users) {
        if(userServer.selectOne(new EntityWrapper<UsersEntity>().eq("username",users.getUsername())) != null) {
            return R.error("读者已存在");
        }
        users.setPassword("123456");
        userServer.insert(users);
        return R.ok("读者信息保存成功！");
    }

    /**
     * 列表
     * @param user
     * @return
     */
    @RequestMapping("/list")
    public R list(UsersEntity user) {
        EntityWrapper<UsersEntity> ew = new EntityWrapper<UsersEntity>();
        ew.allEq(MPUtil.allEQMapPre(user,"user"));
        return R.ok().put("data",userServer.selectListView(ew));
    }

    /**
     * 修改
     * @param users
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody UsersEntity users) {
        userServer.updateById(users);
        return R.ok("修改成功！");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userServer.deleteBatchIds(Arrays.asList(ids));
        return R.ok("删除成功！");
    }
}
